define(['zepto', 'butterfly/view','../../bsl',
    'text!./payment.html'
], function($, View, bsl, payment) {
    var me = null; 

    var view = View.extend({
        events: {
            "tap #back":"back",
            "tap #openDoc":"openDoc",
            
            
        },
        render: function() {
            me = this;
        },

        onShow: function() {
            this.plusReady();

            // 绑定自定义事件 滑动切换日历等           


        },
        pays:{},
        plusReady:function(){
        // 获取支付通道
            bsl.payment.getChannels(function(channels){
                var content=document.getElementById('dcontent');
                var info=document.getElementById("info");
                var txt="支付通道信息：";
                for(var i in channels){
                    var channel=channels[i];
                    pays[channel.id]=channel;
                    txt += "id:"+channel.id+", ";
                    txt += "description:"+channel.description+", ";
                    txt += "serviceReady:"+channel.serviceReady+"； ";
                    var de=document.createElement('div');
                    de.setAttribute('class','button');
                    de.setAttribute('onclick','pay(this.id)');
                    de.id=channel.id;
                    de.innerText=channel.description+"支付";
                    content.appendChild(de);
                    checkServices(channel);
                }
                info.innerText=txt;
            },function(e){
                console.info("获取支付通道失败："+e.message);
            });
        },
        // document.addEventListener('plusready',plusReady,false);
        // 检测是否安装支付服务
        checkServices:function(pc){
            if(!pc.serviceReady){
                var txt=null;
                switch(pc.id){
                    case "alipay":
                    txt="检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
                    break;
                    default:
                    txt="系统未安装“"+pc.description+"”服务，无法完成支付，是否立即安装？";
                    break;
                }
                bsl.nativeUI.confirm(txt,function(e){
                    if(e.index==0){
                        pc.installService();
                    }
                },pc.description);
            }
        },

         w:null,
         PAYSERVER:'http://demo.dcloud.net.cn/payment/?payid=',
        // 支付
        pay:function(id){
            if(w){return;}//检查是否请求订单中
           console.info("----- 请求支付 -----");
            var url=PAYSERVER;
            if(id=='alipay'||id=='wxpay'){
        //      if(id=='wxpay'){
        //          url+='wxpayv3';
        //      }else{
                    url+=id;
        //      }
            }else{
                plus.nativeUI.alert("不支持此支付通道！",null,"捐赠");
                return;
            }
            url+='&appid='+plus.runtime.appid+'&total=';
            
            w=bsl.nativeUI.showWaiting();
            // 请求支付订单
            var amount = document.getElementById('total').value;
            var xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function(){
                switch(xhr.readyState){
                    case 4:
                    w.close();w=null;
                    if(xhr.status==200){
                        console.info("----- 请求订单成功 -----");
                        console.info( xhr.responseText );
                        var order=xhr.responseText;
                        bsl.payment.request(pays[id],order,function(result){
                            console.info("----- 支付成功 -----");
                            bsl.nativeUI.alert("支付成功：感谢你的支持，我们会继续努力完善产品。",function(){
                                back();
                            },"捐赠");
                        },function(e){
                            console.info("----- 支付失败 -----");
                            console.info("["+e.code+"]："+e.message);
                            bsl.nativeUI.alert("更多错误信息请参考支付(Payment)规范文档：http://www.html5plus.org/#specification#/specification/Payment.html",null,"支付失败："+e.code);
                        });
                    }else{
                        console.info("----- 请求订单失败 -----");
                        console.info( xhr.status );
                        bsl.nativeUI.alert("获取订单信息失败！",null,"捐赠");
                    }
                    break;
                    default:
                    break;
                }
            }
            xhr.open('GET',url+amount);
            console.info("请求支付订单："+url+amount);
            xhr.send();
        },
        back : function(e){
            window.history.go(-1);
        },
        openDoc:function(){
            window.butterfly.navigate('com.no5.cordovademo/doc/payment', {
                trigger: true
            });
        }
    });
    return view;
});
